package demo.practice.niuke.zs;

import java.util.Scanner;

public class CD102 {


    public static int view_all_mountains(int arr[]) {
        if (arr == null || arr.length == 1)
            return 0;


        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += view_all_mountains(arr, i);
        }
        return sum;
    }


    public static int view_all_mountains(int arr[], int index) {
        int sum = 0;

        //1正向
        int positive = index;
        int negative = index;
        for (int i = 1; i < arr.length; i++) {
            int curIndex = i + index;
            if (curIndex > arr.length - 1)
                curIndex = curIndex - (arr.length - 1);

            if (arr[index] == arr[curIndex]) {
                positive = curIndex;
                sum++;
            }

            if (arr[index] < arr[curIndex]) {
                positive = curIndex;
                sum++;
                break;
            }
        }

        if (positive == index) //说明根本没找到比自己大的元素，自己是最大的元素，就可以直接返回了
            return sum;


        //2反向
        for (int i = 1; i < arr.length; i++) {
            int curIndex = index - i;
            if (curIndex < 0)
                curIndex = curIndex + (arr.length - 1);


            if (arr[index] == arr[curIndex]) {
                sum++;
                negative = curIndex;
            }

            if (arr[index] < arr[curIndex]) {
                sum++;
                negative = curIndex;
                break;
            }

        }

        if(negative==positive)
            sum--;

        return sum;
    }


    public static void main(String[] args) {
        /*Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int n = sc.nextInt();
            int[] data = new int[n];
            for (int i = 0; i < data.length; i++) {
                data[i] = sc.nextInt();
            }


            System.out.print(view_all_mountains(data));
        }*/

        System.out.println(view_all_mountains(new int[]{5, 5, 2}));
    }
}


